<template>
  <div id="home" class="wrapper">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      class="tab-control"
      @tabClick="tabClick"
      :title="['流行', '新款', '推荐']"
      ref="tabControl1"
      v-show='isShowTabControl'
    />
    <scroll
      ref="scrollview"
      class="scrollcontent"
      :probeType="probeType"
      @scroll="contentScroll"
      :pullUpLoad="pullUpLoad"
      @pullingUp="scrollPullingUp"
    >
      <home-swiper :banners="banners" @swiperImgLoaded='swiperImgLoaded' />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control
        @tabClick="tabClick"
        :title="['流行', '新款', '推荐']"
        ref="tabControl2"
      />
      <goods-list :goods="showGoodsList" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
import { getHomeMultidata, getHomeGoods } from "network/home";
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import Scroll from "components/common/scroll/Scroll";
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";

import { debounce } from "common/utils";
import { mixins,backTopMixin } from 'common/mixin'
 
export default {
  name: "Home",
  data() {
    return {
      banners: null,
      recommends: null,
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      currentType: "pop",
      probeType: 3,
      pullUpLoad: true,
      isShowTabControl: false,
      tabControlOffsetTop: 0,
      scrollY: 0,
      itemImgsListen: null
    };
  },
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,

    NavBar,
    TabControl,
    GoodsList,
    Scroll,
  },
  created: function () {
    this.getHomeMultidata();
    this.getHomeGoods("pop");
    this.getHomeGoods("new");
    this.getHomeGoods("sell");
  },
  methods: {
    getHomeMultidata() {
      getHomeMultidata()
        .then((res) => {
          console.log(res);
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        })
        .catch((err) => console.log(err));
    },
    getHomeGoods(type) {
      let page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        // console.log(res);
        this.goods[type].page = res.data.page;
        this.goods[type].list.push(...res.data.list);
        this.$refs.scrollview.scrollFinished();
      });
    },
    tabClick(index) {
      console.log(index);

      // if(index == 0){
        //   this.type = 'pop'
      // } else if(index == 1){
        //   this.type = 'new'
      // }else if(index == 2){
        //   this.type = 'sell'
      // }
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
      this.getHomeGoods(this.currentType)
      this.$refs.tabControl1.currentIndex = index
      this.$refs.tabControl2.currentIndex = index
    },
    contentScroll(position) {
      // console.log(position)
      this.listenShowBackTop(position)
      this.isShowTabControl = (this.banners.length > 0 && -position.y >= this.tabControlOffsetTop)
      // if(-position.y >= this.tabControlOffsetTop) {
      //   this.isShowTabControl = true
      // } else {
      //   this.isShowTabControl = false
      // }
    },
    contentPullingUp() {
      console.log("----------------");
      this.getHomeGoods(this.currentType);
    },
    scrollPullingUp() {
      this.getHomeGoods(this.currentType);
    },
    // 监听轮播图图片加载完成
    swiperImgLoaded(){
      console.log('123')
      console.log(this.$refs.tabControl2.$el.offsetTop)
      this.tabControlOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },
        
onResize() {
  console.log('尺寸发生变化')
  console.log(this)
  this.scrollRefresh()
},
  },
  computed: {
    showGoodsList() {
      return this.goods[this.currentType].list;
    },
  },
  mixins: [mixins,backTopMixin],
  mounted() {
    
  },
  
  activated(){ 
    this.$refs.scrollview.scrollRefresh()
    // console.log(this.scrollY)
    this.$refs.scrollview.scrollTo(0,this.scrollY,0)
    this.$bus.$on('imgLoaded',this.itemImgsListen)

  },
  deactivated(){
    this.scrollY =  this.$refs.scrollview.getScrollY()
    // console.log(this.scrollY)
    this.$bus.$off('imgLoaded',this.itemImgsListen)
  }
};
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
  /* padding-top: 49px; */
}
.home-nav {
  background-color: var(--color-high-text);
  color: #fff;
  /* position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 9; */
}
.tab-control {
  position: relative;
  z-index: 9;
}
.scrollcontent {
  /* position: fixed;
  top: 49px;
  bottom: 49px;
  left: 0;
  right: 0;
  overflow: hidden;
  padding-bottom: 49px;
  box-sizing: border-box; */
  height: calc(100% - 98px);
  overflow: hidden;
  /* margin-top: 49px; */
  /* margin-bottom: 49px; */
  position: fixed;
  top: 49px;
  bottom: 0px;
}
</style>